import os


def get_file_names(file_dir, types):
    file_list = []
    for root, dirs, files in os.walk(file_dir):
        for file in files:
            if os.path.splitext(file)[1] in types:
                file_list.append(os.path.join(root, file))
    return file_list


def rename_file(file_list_dir, from_tag=0, prefix='', suffix='_', ):
    for index in range(len(file_list_dir)):
        split = os.path.split(file_list_dir[index])
        print('orgin_name:', file_list_dir[index])
        num = from_tag + index
        num = str(num).zfill(3)
        rename_to = prefix + num + suffix + split[1]
        to_rename_dir = split[0] + '\\' + rename_to
        print('rename to:', to_rename_dir)
        os.rename(file_list_dir[index], to_rename_dir)


def add_prefix(file_path, prefix='', suffix='_'):
    # print(file_path)
    print('orgin_name:', file_path)
    split = os.path.split(file_path)
    rename_to = prefix + suffix + split[1]
    to_rename_dir = split[0] + '\\' + rename_to
    print('rename to:', to_rename_dir )

    os.rename(file_path, to_rename_dir)
    return to_rename_dir


def rename_pics(file_dir, from_tag=0, prefix='', suffix='_'):
    file_list = get_file_names(file_dir, {'.jpg', '.JPG'})
    rename_file(file_list, from_tag=from_tag, prefix=prefix, suffix=suffix)


def rename_models(file_dir, from_tag=0, prefix='', suffix='_'):
    file_list = get_file_names(file_dir, {'.psd', '.tif'})
    rename_file(file_list, from_tag=from_tag, prefix=prefix, suffix=suffix)


def de_rename(file_dir):
    # file_list = os.listdir(file_dir)
    for root, dirs, files in os.walk(file_dir):
        for file in files:
            orgin_file = os.path.join(root, file)
            print('orgin_name:', orgin_file)
            strs = os.path.splitext(file)
            name2 = strs[0]
            if name2[2] == "_":
                new_name = name2[3:] + strs[1]
                rename_to_file = os.path.join(root, new_name)
                print('rename to: ' + rename_to_file)
                os.rename(orgin_file, rename_to_file)





if __name__ == "__main__":
    # folder_path = r'D:\temp\结婚展架海报'

    # rename_pics(folder_path, 51, prefix="Z")
    # rename_models(folder_path, 51, prefix="Z")
    # rename_pics(r'D:\temp\temp2', 31)

    folder_path = r'D:\结婚X架模板'
    for root, dirs, files in os.walk(folder_path):
        for file in files:
            file_path = os.path.join(root, file)
            print('orgin_name:', file_path)
            rename_to_file = file_path.replace('Z_', 'Z0')
            print('rename to: ' + rename_to_file)
            # os.rename(file_path, rename_to_file)
            # add_prefix(file_path, 'Z')
            print()


    # de_rename(folder_path)